<template>
  <view class="container">
    <!-- h5 -->
    <!-- <view class="alert">
      <text>访客登记</text>
    </view> -->

    <view class="myForm">
      <uni-forms
        ref="valiForm"
        :rules="rules"
        v-model="formData"
        label-position="top"
        label-width="150"
        :border="false"
        err-show-type="toast"
      >
        <uni-forms-item label="访客姓名" required name="visitorName">
          <uni-easyinput v-model="formData.visitorName" placeholder="请输入" />
        </uni-forms-item>
        <uni-forms-item label="访客身份证号" required name="idCard">
          <uni-easyinput v-model="formData.idCard" placeholder="请输入" />
        </uni-forms-item>
        <uni-forms-item label="联系电话" required name="visitorPhone">
          <uni-easyinput v-model="formData.visitorPhone" placeholder="请输入" />
        </uni-forms-item>
        <uni-forms-item label="来访方式" required name="visitWay">
          <uni-data-checkbox
            v-model="formData.visitWay"
            :localdata="ovisitWay"
          />
        </uni-forms-item>
        <uni-forms-item label="受访人姓名" required name="visitorInterviewee">
          <uni-easyinput
            v-model="formData.visitorInterviewee"
            placeholder="请输入"
          />
        </uni-forms-item>
        <uni-forms-item label="受访人电话" required name="intervieweePhone">
          <uni-easyinput
            v-model="formData.intervieweePhone"
            placeholder="请输入"
          />
        </uni-forms-item>
        <uni-forms-item label="来访时间" required name="visitorTime">
          <uni-datetime-picker
            type="datetime"
            hide-second
            :start="new Date().getTime()"
            v-model="formData.visitorTime"
          />
        </uni-forms-item>
        <uni-forms-item label="备注" name="remark">
          <uni-easyinput v-model="formData.remark" placeholder="请输入" />
        </uni-forms-item>

        <uni-popup
          ref="popup"
          borderRadius="40rpx 40rpx 0 0"
          background-color="#fff"
        >
          <view class="secondBpx">
            <view class="tip">
              <view class="n">安保人员需与受访人进行探访确认</view>
              <view class="v">受访人电话：{{ formData.intervieweePhone }}</view>
            </view>
            <uni-forms-item
              label="是否与受访人经过确认"
              required
              name="isIntervieweeConfirmed"
            >
              <uni-data-checkbox
                v-model="formData.isIntervieweeConfirmed"
                :localdata="isYesNo"
              ></uni-data-checkbox>
            </uni-forms-item>
            <uni-forms-item
              label="安保人员姓名"
              name="visitorSecurity"
              required
            >
              <uni-easyinput
                v-model="formData.visitorSecurity"
                placeholder="请输入"
              />
            </uni-forms-item>
            <button class="btn" type="primary" @click="onSubmit('valiForm')">
              提交
            </button>
          </view>
        </uni-popup>
      </uni-forms>
    </view>
    <view class="btnBox">
      <button class="btn" type="primary" @click="onNext('valiForm')">
        提交
      </button>
    </view>
  </view>
</template>

<script>
import { getUserAllList } from '@/api/index';
import { addVisitor } from '@/api/visitor';
import { getVisitorInviteDetail } from '@/api/workOrder';
import useAppStore from '@/store/modules/app';
export default {
  components: {},
  data() {
    return {
      isYesNo: [
        {
          text: '是',
          label: '是',
          value: '1',
        },
        {
          text: '否',
          label: '否',
          value: '0',
        },
      ],
      query: {},
      isShow: false,
      // 表单数据ovisitWay
      ovisitWay: [
        {
          label: '钉钉',
          text: '钉钉',
          value: '1',
        },
        {
          label: '扫码预约',
          text: '扫码预约',
          value: '2',
        },
        {
          label: '邀约',
          text: '邀约',
          value: '3',
        },
        {
          label: '未预约',
          text: '未预约',
          value: '4',
        },
      ],
      formData: {},
      // 规则
      rules: {
        visitorName: {
          rules: [
            {
              required: true,
              errorMessage: '访客姓名不能为空',
            },
          ],
        },
        idCard: {
          rules: [
            {
              required: true,
              errorMessage: '访客身份证号不能为空',
            },
            {
              pattern:
                /^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(10|11|12))([0-2]\d|30|31)\d{3}[0-9Xx]$/,
              errorMessage: '请输入正确的身份证号',
            },
          ],
        },
        visitorInterviewee: {
          rules: [
            {
              required: true,
              errorMessage: '请输入受访人内容',
            },
          ],
        },
        visitorSecurity: {
          rules: [
            {
              required: false,
              errorMessage: '请输入安保人员内容',
            },
          ],
        },
        visitorTime: {
          rules: [
            {
              required: true,
              errorMessage: '请选择日期',
            },
          ],
        },
        visitWay: {
          rules: [
            {
              required: true,
              errorMessage: '请选择方式',
            },
          ],
        },
        isIntervieweeConfirmed: {
          rules: [
            {
              required: true,
              errorMessage: '请选择是否确认',
            },
          ],
        },
        visitorPhone: {
          rules: [
            {
              required: true,
              errorMessage: '请输入手机号',
            },
            {
              pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
              errorMessage: '请输入正确的手机号码',
            },
          ],
        },
        intervieweePhone: {
          rules: [
            {
              required: true,
              errorMessage: '请输入手机号',
            },
            {
              pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
              errorMessage: '请输入正确的手机号码',
            },
          ],
        },
      },
      // store
      appStore: useAppStore(),
    };
  },
  watch: {
    'formData.date': {
      handler(newVal) {},
      immediate: false,
      deep: true,
    },
  },
  onLoad(query) {
    this.query = query;
    // if (this.query.id) this.getData();
    // this.$util.timestampToTime(new Date().getTime())
  },
  methods: {
    // 获取数据
    async getData() {
      let personData = [];
      await getUserAllList().then((res) => {
        personData = res.data;
      });
      getVisitorInviteDetail(this.query.id).then((res) => {
        this.formData = {
          ...this.formData,
          visitWay: '2',
          visitorName: res.data.visitorName,
          visitorPhone: res.data.visitorPhone,
          visitorInterviewee: personData.filter(
            (item) => item.userId === res.data?.entourageUserId
          )[0]['nickName'],
        };
      });
    },
    // 提交
    onNext(ref) {
      this.$refs[ref]
        .validate()
        .then((res) => {
          // 成功返回，res 为对应表单数据
          this.$refs.popup.open('bottom');
        })
        .catch((err) => {
          // 表单校验验失败，err 为具体错误信息
        });
    },
    onSubmit(ref) {
      this.$refs[ref]
        .validate()
        .then((res) => {
          console.log('success', res);
          // loading提示
          uni.showLoading({ title: '提交中' });
          // 提交接口
          let params = {
            ...res,
            visitorTime:
              res.visitorTime.length === 19
                ? res.visitorTime
                : res.visitorTime + ':00',
          };
          addVisitor(params).then((res) => {
            uni.hideLoading();
            this.$refs.popup.close();
            uni.showToast({
              title: `提交成功`,
            });
            // 重置
            this.onReset();
            this.onBack();
          });
        })
        .catch((err) => {
          console.log('err', err);
          this.$refs.popup.close();
        });
    },
    // 重置表单
    onReset() {
      this.formData = this.$options.data().formData;
    },
    // 跳转页面
    onLocation(url) {
      uni.navigateTo({
        url,
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.container {
  min-height: calc(100vh - var(--window-bottom) - 200rpx);
  padding: 30rpx 36rpx calc(var(--window-bottom) + 70px);
  display: flex;
  flex-direction: column;

  .alert {
    height: 50px;
    margin: 20px 0;
    text-align: center;
    background: #ddeafa;
    border-radius: 12px;
    display: flex;
    justify-content: center;
    align-items: center;
    text {
      // font-size: 32rpx;
      font-size: 24px;
      font-weight: 600;
      color: #3a84ff;
    }
  }

  .myForm {
    padding: 24rpx;
    background: #fff;
    border-radius: 12rpx;

    ::v-deep .uni-forms-item__label {
      width: 50%;
      color: #333333;
      // font-size: 28rpx;
      font-size: 20px;
      font-weight: bold;
      position: relative;
      uni-text {
        white-space: nowrap;
      }

      &::after {
        // content: "";
        width: 100%;
        height: 1rpx;
        background: #000;
        position: absolute;
        left: 14rpx;
        bottom: 20rpx;
      }
    }

    .myLabel {
      display: flex;
      align-items: center;
      justify-content: space-between;
      .text {
        color: #333333;
        font-size: 28rpx;
        font-weight: bold;
      }
      .star {
        color: #dd524d;
      }
    }
  }

  .secondBpx {
    padding: 40rpx;
    .tip {
      .n {
        font-size: 38rpx;
        font-weight: 600;
      }
      .v {
        color: #989898;
        padding: 10rpx 0 15rpx;
      }
    }
  }

  .btnBox {
    position: fixed;
    left: 36px;
    right: 36px;
    bottom: calc(var(--window-bottom) + 20px);
    .btn {
      width: 100%;
      // height: 92rpx;
      height: 40px;
      line-height: 40px;
      // font-size: 40rpx;
      font-size: 24px;
      color: #ffffff;
      background: #3681ff;
    }
  }
}
@media (min-width: 500px) and (max-width: 1366px) {
  .container {
    min-height: calc(100vh - var(--window-bottom) - 200rpx);
    padding: 0rpx 36rpx calc(var(--window-bottom) + 70px);
    display: flex;
    flex-direction: column;

    .alert {
      height: 50px;
      margin: 20px 0;
      text-align: center;
      background: #ddeafa;
      border-radius: 12px;
      display: flex;
      justify-content: center;
      align-items: center;
      text {
        // font-size: 32rpx;
        font-size: 24px;
        font-weight: 600;
        color: #3a84ff;
      }
    }

    .myForm {
      padding: 24rpx;
      background: #fff;
      border-radius: 12rpx;

      ::v-deep .uni-forms-item__label {
        width: 50%;
        color: #333333;
        // font-size: 28rpx;
        font-size: 28px;
        font-weight: bold;
        position: relative;
        margin: 5px 0 10px;
        uni-text {
          white-space: nowrap;
        }

        &::after {
          // content: "";
          width: 100%;
          height: 1rpx;
          background: #000;
          position: absolute;
          left: 14rpx;
          bottom: 20rpx;
        }
      }
      ::v-deep .uni-forms-item__content {
        .uni-easyinput__content-input,
        .uni-date__x-input {
          height: 50px;
          line-height: 50px;
          font-size: 24px;
        }
        .uni-input-placeholder.uni-easyinput__placeholder-class {
          font-size: 20px;
        }
        .checklist-text {
          font-size: 24px !important;
        }
        .uni-date-single--x {
          transform: scale(1.3);
          transform-origin: 0 0;
        }
        .uni-icons {
          font-size: 30px;
        }
      }

      .myLabel {
        display: flex;
        align-items: center;
        justify-content: space-between;
        .text {
          color: #333333;
          font-size: 28rpx;
          font-weight: bold;
        }
        .star {
          color: #dd524d;
        }
      }
    }

    .secondBpx {
      padding: 40rpx;
      .tip {
        .n {
          font-size: 38rpx;
          font-weight: 600;
        }
        .v {
          color: #989898;
          padding: 10rpx 0 15rpx;
        }
      }
    }

    .btnBox {
      position: fixed;
      left: 36px;
      right: 36px;
      bottom: calc(var(--window-bottom) + 50px);
      .btn {
        width: 100%;
        // height: 92rpx;
        height: 60px;
        line-height: 60px;
        // font-size: 40rpx;
        font-size: 24px;
        color: #ffffff;
        background: #3681ff;
      }
    }
  }
}
</style>
